package com.example.myapplication.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.VideoBean;
import com.squareup.picasso.Picasso;

public class ItemMain2Adapter extends BaseAdapter {

    private List<VideoBean> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemMain2Adapter(Context context, List<VideoBean> objects) {
        this.objects=objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_main2, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder)convertView.getTag();
        }
        VideoBean videoBean=objects.get(position);
        holder.moviseTitle.setText(videoBean.getTitle());
        Uri uri = Uri.parse( videoBean.getPlayurl());
        //设置视频控制器
        holder.videoView.setMediaController(new MediaController(context));
        //播放完成回调
        holder.videoView.setOnCompletionListener(new MyPlayerOnCompletionListener());
        //设置视频路径
        holder.videoView.setVideoURI(uri);
        holder.sorceTitle.setText(videoBean.getSorce_title());
        if (!TextUtils.isEmpty(videoBean.getUrl())) {
            Picasso.get().load(videoBean.getUrl()).into(holder.icon);
        }
        holder.movesLove.setText(videoBean.getMoves_love());
        holder.movesTalk.setText(videoBean.getMoves_talk());
        //开始播放视频
//                videoView.start();

        return convertView;
    }
    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText( context, "播放完成了", Toast.LENGTH_SHORT).show();
        }
    }
    protected class ViewHolder {
        private TextView moviseTitle;
    private VideoView videoView;
    private ImageView icon;
    private TextView sorceTitle;
    private ImageView talk;
    private TextView movesTalk;
    private ImageView love;
    private TextView movesLove;

        public ViewHolder(View view) {
            moviseTitle = (TextView) view.findViewById(R.id.movise_title);
            videoView = (VideoView) view.findViewById(R.id.videoView);
            icon = (ImageView) view.findViewById(R.id.icon);
            sorceTitle = (TextView) view.findViewById(R.id.sorce_title);
            talk = (ImageView) view.findViewById(R.id.talk);
            movesTalk = (TextView) view.findViewById(R.id.moves_talk);
            love = (ImageView) view.findViewById(R.id.love);
            movesLove = (TextView) view.findViewById(R.id.moves_love);
        }
    }
}
